package com.android.delivery.activity

import android.os.Bundle
import android.text.TextUtils
import com.ada.common.util.CustomHelper
import com.android.delivery.App
import com.android.delivery.R
import com.android.delivery.api.ApiService
import com.android.delivery.callback.DialogCallback
import com.android.delivery.callback.LzyResponse
import com.android.delivery.entity.FileResult
import com.android.delivery.entity.User
import com.android.delivery.entity.even.RefreshUserEvent
import com.android.delivery.util.GlideUtil
import com.android.delivery.util.ToastUtils
import com.android.delivery.widget.PhotoPopup
import com.android.delivery.widget.SexPopup
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.interfaces.SimpleCallback
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.activity_person.*
import org.devio.takephoto.app.TakePhotoActivity
import org.devio.takephoto.model.TResult
import org.greenrobot.eventbus.EventBus
import java.io.File


/**
 * @author: mqwang
 * @create: 2/24/21 10:54 AM
 * @Description:
 */
class PersonActivity : TakePhotoActivity() {
    private var sex: Int = 0
    private var imgsrc: String? = ""
    private var user: User? = null
    private var sexDialog: BasePopupView? = null
    private var picDialog: BasePopupView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_person)
        common_toolbar.setNavigationOnClickListener {
            finish()
        }

        ApiService.user_info(this, object : DialogCallback<LzyResponse<User>>() {
            override fun onSuccess(response: Response<LzyResponse<User>>?) {
                user = response?.body()?.data
                if (user != null) {
                    GlideUtil.loadCircleImg(iv_image, user?.avatar)

                    val nickname = user?.nickname
                    if (!TextUtils.isEmpty(nickname)) {
                        et_name.setText(nickname)
                        et_name.setSelection(nickname!!.length)
                    }
                    et_phone.setText(user?.mobile)
                    if (user?.sex != null) {
                        sex = user?.sex!!
                    }
                    setSex()
                }
            }
        })

        layout_photo.setOnClickListener {
            picDialog = XPopup.Builder(this)
                    .autoOpenSoftInput(true)
                    .setPopupCallback(object : SimpleCallback() {
                        override fun onDismiss(popupView: BasePopupView?) {
                            super.onDismiss(popupView)
                        }
                    })
                    .asCustom(PhotoPopup(this, object : PhotoPopup.Onclick {
                        override fun first() {
                            picDialog?.dismiss()
                            CustomHelper.TakePhoto(takePhoto, CustomHelper.PICK_BY_TAKE, false)
                        }

                        override fun second() {
                            picDialog?.dismiss()
                            CustomHelper.TakePhoto(takePhoto, CustomHelper.PICK_BY_SELECT, false)
                        }
                    }))
                    .show()
        }

        layout_sex.setOnClickListener {
            sexDialog = XPopup.Builder(this)
                    .autoOpenSoftInput(true)
                    .setPopupCallback(object : SimpleCallback() {
                        override fun onDismiss(popupView: BasePopupView?) {
                            super.onDismiss(popupView)
                        }
                    })
                    .asCustom(SexPopup(this, object : SexPopup.Onclick {
                        override fun first() {
                            sexDialog?.dismiss()
                            sex = 1
                            setSex()
                        }

                        override fun second() {
                            sexDialog?.dismiss()
                            sex = 2
                            setSex()
                        }
                    }))
                    .show()

        }

        tv_sub_title.setOnClickListener {
            var name = et_name.text.toString()
            var phone = et_phone.text.toString()

            ApiService.user_modify(this, user?.id, imgsrc, name, phone, sex, object : DialogCallback<LzyResponse<Any>>() {
                override fun onSuccess(response: Response<LzyResponse<Any>>?) {
                    EventBus.getDefault().post(RefreshUserEvent())
                    ToastUtils.showShort(this@PersonActivity, getString(R.string.success))
                    finish()
                }

            })
        }

    }

    fun setSex() {
        if (sex == 0) {
            tv_sex.setText("未知")
        } else if (sex == 1) {
            tv_sex.setText("男")
        } else {
            tv_sex.setText("女")
        }
    }


    override fun takeSuccess(result: TResult?) {
        super.takeSuccess(result)
        ApiService.uploader(this, File(result!!.image.compressPath), object : DialogCallback<LzyResponse<FileResult>>() {
            override fun onSuccess(response: Response<LzyResponse<FileResult>>?) {
                val data = response?.body()?.data
                if (data != null) {
                    imgsrc = data.url
                    user?.avatar = imgsrc
                    val loginInfo = App.getInstance()?.getLoginInfo();
                    if (loginInfo != null) {
                        loginInfo.user?.avatar = imgsrc
                        App.getInstance()?.setLoginInfo(loginInfo)
                    }
                    GlideUtil.loadCircleImg(iv_image, user?.avatar)
                }
            }
        })
    }

}